package com.wanho.qz103.hrsaas.sys.dao.impl;

import com.wanho.qz103.hrsaas.commons.annontations.Repository;
import com.wanho.qz103.hrsaas.commons.dao.impl.BaseDAOImpl;
import com.wanho.qz103.hrsaas.commons.exception.DAOException;
import com.wanho.qz103.hrsaas.commons.exception.DataAccessException;
import com.wanho.qz103.hrsaas.commons.query.Page;
import com.wanho.qz103.hrsaas.sys.dao.UserDAO;
import com.wanho.qz103.hrsaas.sys.pojos.User;

import java.util.List;

@Repository
public class UserDAOImpl extends BaseDAOImpl<User> implements UserDAO {
    @Override
    public boolean insertBatch(List<User> userList) throws DAOException {
        try {
            // 因为 前端 vue excel 组件 上传 日期格式问题： 故 先放弃 日期处理
            String sql = "INSERT INTO sys_user_info ( id, mobile, username, salt, PASSWORD, enableState, timeOfEntry, correctionTime, workNumber )\n" +
                    "VALUES\n" +
                    "\t(?,?,?,?,?,?,now(),now(),?)" ;
            Object[][] paramAy = new Object[userList.size()][7] ;
            for (int i = 0; i < userList.size(); i++) {
                User user = userList.get(i);
                paramAy[i][0] =user.getId() ;
                paramAy[i][1] = user.getMobile() ;
                paramAy[i][2] = user.getUsername() ;
                paramAy[i][3] = user.getSalt() ;
                paramAy[i][4] = user.getPassword() ;
                paramAy[i][5] = user.getEnableState() ;
                paramAy[i][6] = user.getWorkNumber() ;
            }
            return jdbcTemplate.insertBatch(sql,paramAy);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException("批量插入用户表数据异常！！！") ;
        }
    }

    @Override
    protected String selectCountSQL() {
        return "select count(*) from sys_user_info";
    }

    @Override
    protected String selectTopNSQL(Page page) {
        return "select id,\n" +
                "    mobile,\n" +
                "    username,\n" +
                "    salt,\n" +
                "    password,\n" +
                "    enableState,\n" +
                "    createTime,\n" +
                "    companyId,\n" +
                "    companyName,\n" +
                "    departmentId,\n" +
                "    timeOfEntry,\n" +
                "    formOfEmployment,\n" +
                "    workNumber,\n" +
                "    formOfManagement,\n" +
                "    workingCity,\n" +
                "    correctionTime,\n" +
                "    inServiceStatus,\n" +
                "    departmentName,\n" +
                "    staffPhoto from sys_user_info limit #{offset},#{size}";
    }

    /**
     * 返回用户简单列表
     * @return
     */
    @Override
    protected String selectAllSQL() {
        return "SELECT id,\n" +
                "       username\n" +
               " FROM sys_user_info ";
    }



    @Override
    public User selectByMobile(String mobile) throws DAOException {
        try {
            String sql = "SELECT " +
                    "       id,\n" +
                    "       salt,\n" +
                    "       password\n" +
                    "FROM sys_user_info\n" +
                    "      where mobile = ? " ;
            Object[] paramAy = {mobile} ;
            return jdbcTemplate.queryObject(sql,User.class,paramAy);
        } catch (DataAccessException e) {
            e.printStackTrace();
            throw new DAOException(e.getMessage()) ;
        }
    }

    @Override
    protected String deleteSQL() {
        return null;
    }

    @Override
    protected String insertSQL() {
        return null ;
    }

    @Override
    protected String updateSQL() {
        return "UPDATE sys_user_info \n" +
                "SET mobile = #{mobile},\n" +
                "username = #{username},\n" +
                "salt = #{salt},\n" +
                "PASSWORD = #{password},\n" +
                "enableState = #{enableState},\n" +
                "createTime = #{createTime},\n" +
                "companyId = #{companyId},\n" +
                "companyName = #{companyName},\n" +
                "departmentId = #{departmentId},\n" +
                "timeOfEntry = #{timeOfEntry},\n" +
                "formOfEmployment = #{formOfEmployment},\n" +
                "workNumber = #{workNumber},\n" +
                "formOfManagement = #{formOfManagement},\n" +
                "workingCity = #{workingCity},\n" +
                "correctionTime = #{correctionTime},\n" +
                "inServiceStatus = #{inServiceStatus},\n" +
                "departmentName = #{departmentName},\n" +
                "staffPhoto = #{staffPhoto}\n" +
                "WHERE\n" +
                "\tid = #{id}";
    }

    @Override
    protected String selectByIdSQL() {
        return "SELECT id,\n" +
                "       mobile,\n" +
                "       username,\n" +
                "       salt,\n" +
                "       password,\n" +
                "       enableState,\n" +
                "       createTime,\n" +
                "       companyId,\n" +
                "       companyName,\n" +
                "       departmentId,\n" +
                "       timeOfEntry,\n" +
                "       formOfEmployment,\n" +
                "       workNumber,\n" +
                "       formOfManagement,\n" +
                "       workingCity,\n" +
                "       correctionTime,\n" +
                "       inServiceStatus,\n" +
                "       departmentName,\n" +
                "       staffPhoto\n" +
                "FROM sys_user_info where id = #{id}";
    }
}
